package com.general.framework.core.lang;

import com.general.framework.core.utils.SnowflakeIdWorker;
import org.apache.commons.lang3.RandomUtils;

import java.util.UUID;

/**
 * @author leejean
 * @ClassName: IdSeqTakes
 * @Description: ID生成工具类
 * @date 2020年05月20 16:26:59
 */
public class IdTakes {

    public static final SnowflakeIdWorker SNOWFLAKE_ID_WORKER = new SnowflakeIdWorker(8);

    /**
     * 数字池
     */
    public final static String NUMBER_POOL = "0123456789";

    /**
     * 字母池
     */
    public final static String LETTER_POOL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 获取雪花ID
     *
     * @return
     */
    public static Long takeSnowflakeId() {
        return SNOWFLAKE_ID_WORKER.nextId();
    }

    /**
     * 获取UUID
     *
     * @return
     */
    public static String takeUUID() {
        return (UUID.randomUUID().toString()).replace("-", "");
    }

    /**
     * 获取随机数字
     *
     * @param length
     * @return
     */
    public static String takeRandomNum(int length) {
        return takeRandomCode(NUMBER_POOL, length);
    }

    /**
     * 获取随机号
     *
     * @param length
     * @return
     */
    public static String takeRandomCode(int length) {
        final String src = NUMBER_POOL + LETTER_POOL + LETTER_POOL.toLowerCase();
        return takeRandomCode(src, length);
    }

    /**
     * 生成随机码
     *
     * @param src    源字符串
     * @param length
     * @return
     */
    public static String takeRandomCode(final String src, final int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0, len = src.length(); i < length; i++) {
            int index = RandomUtils.nextInt(0, len);
            sb.append(src.charAt(index));
        }
        return sb.toString();
    }

}
